using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Zephyris_Space_Mercs
{
    class SettingsMenu
    {
        SettingsFileHandler settingsFileHandler = new SettingsFileHandler();
        SettingsData settingsData = new SettingsData();

        int timeCounter = 0;

        List<ClickableRegion> clickableRegions = new List<ClickableRegion>();

        UI interfaceUI;
        SpriteFont uiFont;
        Texture2D uiBackground;
        Texture2D uiButton;

        GraphicsDevice device;

        // Settings values
        public bool Music = false;
        public bool SFX = false;
        public bool InvertY = false;
        public bool CloudSync = false;

        // Settings values
        public bool oldMusic = false;
        public bool oldSFX = false;
        public bool oldInvertY = false;
        public bool oldCloudSync = false;


        private string Bool2Str(bool setting)
        {
            if (setting)
                return "ON";
            else
                return "OFF";
        }

        public void Init(SpriteBatch spriteBatch, SpriteBatch uiBatch, GraphicsDevice device)
        {
            interfaceUI = new UI(spriteBatch, uiBatch);
            this.device = device;

            GetSettings();
        }

        public void LoadContent(Texture2D uiBackground, Texture2D uiButton, SpriteFont uiFont)
        {
            this.uiBackground = uiBackground;
            this.uiButton = uiButton;
            this.uiFont = uiFont;
        }

        public void DrawScreen()
        {
            timeCounter++;
            if (timeCounter > Constants.timeCounterVal)
                timeCounter = 0;

            interfaceUI.drawScreen(uiBackground);
            clickableRegions.Clear();

            Vector2 topleft = new Vector2(0, 0);
            Vector2 bottomright = new Vector2(0, 0);

            topleft = new Vector2(20 + (this.uiButton.Width / 2), (device.Viewport.Height / 2) - uiButton.Height + 120);
            bottomright = new Vector2(topleft.X + this.uiButton.Width, topleft.Y + this.uiButton.Height);
            interfaceUI.drawButton(uiButton, topleft, uiFont, "   Save");
            AddClickable(topleft, bottomright);

            topleft = new Vector2(device.Viewport.Width - uiButton.Width - 20 - (this.uiButton.Width / 2), topleft.Y);
            bottomright = new Vector2(topleft.X + this.uiButton.Width, topleft.Y + this.uiButton.Height);
            interfaceUI.drawButton(uiButton, topleft, uiFont, "   Cancel");
            AddClickable(topleft, bottomright);

            Vector2 textOffset = new Vector2(0, 0);

            textOffset = new Vector2(20, 20);
            interfaceUI.drawText(textOffset, uiFont, "Music");
            interfaceUI.drawText(new Vector2(device.Viewport.Width - 100, textOffset.Y) , uiFont, Bool2Str(Music));
            AddClickable(textOffset, new Vector2(device.Viewport.Width, textOffset.Y + 40));

            textOffset = new Vector2(20, 80);
            interfaceUI.drawText(textOffset, uiFont, "SFX");
            interfaceUI.drawText(new Vector2(device.Viewport.Width - 100, textOffset.Y), uiFont, Bool2Str(SFX));
            AddClickable(textOffset, new Vector2(device.Viewport.Width, textOffset.Y + 40));

            textOffset = new Vector2(20, 140);
            interfaceUI.drawText(textOffset, uiFont, "Invert Y-Axis");
            interfaceUI.drawText(new Vector2(device.Viewport.Width - 100, textOffset.Y), uiFont, Bool2Str(InvertY));
            AddClickable(textOffset, new Vector2(device.Viewport.Width, textOffset.Y + 40));

            textOffset = new Vector2(20, 200);
            interfaceUI.drawText(textOffset, uiFont, "Cloud Sync");
            interfaceUI.drawText(new Vector2(device.Viewport.Width - 100, textOffset.Y), uiFont, Bool2Str(CloudSync));
            AddClickable(textOffset, new Vector2(device.Viewport.Width, textOffset.Y + 40));
        }

        public void AddClickable(Vector2 topleftCorner, Vector2 bottomrightCorner)
        {
            ClickableRegion clickableRegion = new ClickableRegion();
            clickableRegion.TopLeftCorner = topleftCorner;
            clickableRegion.BottomRightCorner = bottomrightCorner;
            clickableRegions.Add(clickableRegion);
        }

        public UIPages.UIPage CheckClick(Vector2 clickedRegion)
        {
            if (timeCounter > Constants.timeCounterVal / 2)
            {
                for (int i = 0; i < clickableRegions.Count; i++)
                {
                    switch (i)
                    {
                        case 0:
                            if ((clickedRegion.X > clickableRegions[i].TopLeftCorner.X) && (clickedRegion.X < clickableRegions[i].BottomRightCorner.X)
                                &&
                                (clickedRegion.Y > clickableRegions[i].TopLeftCorner.Y) && (clickedRegion.Y < clickableRegions[i].BottomRightCorner.Y))
                            {
                                ApplySettings();
                                GetSettings();
                                timeCounter = 0;
                                return UIPages.UIPage.MainMenu;
                            }
                            break;

                        case 1:
                            if ((clickedRegion.X > clickableRegions[i].TopLeftCorner.X) && (clickedRegion.X < clickableRegions[i].BottomRightCorner.X)
                               &&
                               (clickedRegion.Y > clickableRegions[i].TopLeftCorner.Y) && (clickedRegion.Y < clickableRegions[i].BottomRightCorner.Y))
                            {
                                RevertSettings();
                                timeCounter = 0;
                                return UIPages.UIPage.MainMenu;
                            }
                            break;

                        case 2:
                            if ((clickedRegion.X > clickableRegions[i].TopLeftCorner.X) && (clickedRegion.X < clickableRegions[i].BottomRightCorner.X)
                               &&
                               (clickedRegion.Y > clickableRegions[i].TopLeftCorner.Y) && (clickedRegion.Y < clickableRegions[i].BottomRightCorner.Y))
                            {
                                Music = !Music;
                                timeCounter = 0;
                                return UIPages.UIPage.SettingsMenu;
                            }
                            break;

                        case 3:
                            if ((clickedRegion.X > clickableRegions[i].TopLeftCorner.X) && (clickedRegion.X < clickableRegions[i].BottomRightCorner.X)
                               &&
                               (clickedRegion.Y > clickableRegions[i].TopLeftCorner.Y) && (clickedRegion.Y < clickableRegions[i].BottomRightCorner.Y))
                            {
                                SFX = !SFX;
                                timeCounter = 0;
                                return UIPages.UIPage.SettingsMenu;
                            }
                            break;

                        case 4:
                            if ((clickedRegion.X > clickableRegions[i].TopLeftCorner.X) && (clickedRegion.X < clickableRegions[i].BottomRightCorner.X)
                               &&
                               (clickedRegion.Y > clickableRegions[i].TopLeftCorner.Y) && (clickedRegion.Y < clickableRegions[i].BottomRightCorner.Y))
                            {
                                InvertY = !InvertY;
                                timeCounter = 0;
                                return UIPages.UIPage.SettingsMenu;
                            }
                            break;

                        case 5:
                            if ((clickedRegion.X > clickableRegions[i].TopLeftCorner.X) && (clickedRegion.X < clickableRegions[i].BottomRightCorner.X)
                               &&
                               (clickedRegion.Y > clickableRegions[i].TopLeftCorner.Y) && (clickedRegion.Y < clickableRegions[i].BottomRightCorner.Y))
                            {
                                CloudSync = !CloudSync;
                                timeCounter = 0;
                                return UIPages.UIPage.SettingsMenu;
                            }
                            break;

                    }
                }
            }
            return UIPages.UIPage.SettingsMenu;
        }

        private void ApplySettings()
        {
            settingsData.Music = this.Music;
            settingsData.SFX = this.SFX;
            settingsData.InvertY = this.InvertY;
            settingsData.CloudSync = this.CloudSync;
            settingsFileHandler.SaveData(settingsData);
        }

        private void GetSettings()
        {
            settingsData = settingsFileHandler.ReadData();
            this.oldMusic = this.Music = settingsData.Music;
            this.oldSFX = this.SFX = settingsData.SFX;
            this.oldInvertY = this.InvertY = settingsData.InvertY;
            this.oldCloudSync = this.CloudSync = settingsData.CloudSync;
        }

        private void RevertSettings()
        {
            this.Music = this.oldMusic;
            this.SFX = this.oldSFX;
            this.InvertY = this.oldInvertY;
            this.CloudSync = this.oldCloudSync;
        }
    }
}
